package mesweb.mesweb.controller.produce;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.produce.ProduceCalendar;
import mesweb.mesweb.service.produce.IProduceCalendarService;
import mesweb.mesweb.vo.mapping.ProduceCalendarMapping;
import mesweb.mesweb.vo.produce.ProduceCalendarVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 生产日历
 * @Author: jeecg-boot
 * @Date:   2021-08-21
 * @Version: V1.0
 */

@Api(tags="生产日历")
@RestController
@RequestMapping("/mes/produceCalendar")
public class ProduceCalendarController {
	@Autowired
	private IProduceCalendarService produceCalendarService;

	@Autowired
	private ProduceCalendarMapping produceCalendarMapping;

    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<ProduceCalendar> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = produceCalendarService.list();
        } else {
            QueryWrapper<ProduceCalendar> queryWrapper = new QueryWrapper<>();
            data = produceCalendarService.list(queryWrapper);
        }
        return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param produceCalendar
	 * @param page
	 * @param size
	 * @param req
	 * @return
	 */
	@ApiOperation(value="生产日历-分页列表查询", notes="生产日历-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(ProduceCalendar produceCalendar,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapper<ProduceCalendarVo> queryWrapper = new QueryWrapper<>();
		Page<ProduceCalendarVo> pageProduceCalendar = new Page<ProduceCalendarVo>(page, size);
		IPage<ProduceCalendarVo> pageList = produceCalendarService.getProduceCalendars(pageProduceCalendar, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param produceCalendar
	 * @return
	 */
	@ApiOperation(value="生产日历-编辑", notes="生产日历-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody ProduceCalendarVo produceCalendar) {

		boolean result = false;
        if(produceCalendar.getId() == 0L) {
			QueryWrapper<ProduceCalendar> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("Calendar_Name", produceCalendar.getCalendarName());
			ProduceCalendar mesTechnologyFlow1 = produceCalendarService.getOne(queryWrapper);
			if (mesTechnologyFlow1 != null) {
				String msg = StrUtil.format("名称{}已经存在！", mesTechnologyFlow1.getCalendarName());
				throw new ApiException(msg);
			}
             result = produceCalendarService.save(produceCalendarMapping.toProduceCalendar(produceCalendar));
        }
        else
        {
            result = produceCalendarService.updateById(produceCalendarMapping.toProduceCalendar(produceCalendar));
        }
        return CommonResult.success(result);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="生产日历-通过id删除", notes="生产日历-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		produceCalendarService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="生产日历-批量删除", notes="生产日历-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("ProduceCalendarIDs") @RequestBody List<Long> ids) {
		this.produceCalendarService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="生产日历-通过id查询", notes="生产日历-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		ProduceCalendar produceCalendar = produceCalendarService.getById(id);
		return CommonResult.success(produceCalendar);
	}

	/**
	 * 获取当前月的工作日数
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="生产日历-获取当前月的工作日数", notes="获取当前月的工作日数")
	@GetMapping(value = "/getMonthWorkhours")
	public CommonResult<?> getMonthWorkhours(@RequestParam(name="id",required=true,defaultValue = "0") Long id,
											 @RequestParam(name="date", required= true) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
		Float result = produceCalendarService.getMonthWorkhours(date, id);
		return CommonResult.success(result);
	}

}
